//
//  XJCmtIdentityVC.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/13.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  
//  社区认证VC

#import "XJCmtIdentityVC.h"
#import "XJCmtIdCommitMsgView.h" // 提交审核信息view
#import "XJCmtIdResultView.h" // 审核状态(审核中/审核通过)
@interface XJCmtIdentityVC ()
@property (nonatomic, strong)XJCmtIdCommitMsgView *commitMsgView;
@property (nonatomic, strong) XJCmtIdResultView *cmtResltView;


@end

@implementation XJCmtIdentityVC
#pragma mark - 一 lazy
- (XJCmtIdCommitMsgView *)commitMsgView {
    if (!_commitMsgView) {
        _commitMsgView = [[XJCmtIdCommitMsgView alloc] initWithFrame:self.view.bounds];
        XJWeakSelf(self);
        
        //网络请求是放在viw层中的。这个block被调用说明请求已经成功
        _commitMsgView.xjcmtIdCommitMsgViewClickSubmitBtnBlock = ^{
            //直接原路返回即可
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _commitMsgView;
}
- (XJCmtIdResultView *)cmtResltView {
    if (!_cmtResltView) {
        _cmtResltView = [[XJCmtIdResultView alloc] initWithFrame:self.view.bounds];
    }
    return _cmtResltView;
}

#pragma mark - 二 请求参数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"社区认证";
    
    [self.view addSubview:self.commitMsgView];
    
    self.cmtResltView.hidden = YES;
    [self.view addSubview:self.cmtResltView];
    
    XJLog(@"社区id是:%@",self.communityId);
    
    self.commitMsgView.community_id = self.communityId; //请求参数✅
    [self requestIsCummunityIdentity:self.communityId];
}


#pragma mark - 三 逻辑
/**
 commitMsgView;
 
 cmtResltView;

 根据请求结果控制 上述两个view 的 hidden属性
 
 if (实名认证) { //是
 
    commitMsgView 隐藏
    cmtResltView 继续调用第二个接口. 根据服务器返回的数据进行赋值。
 }else { 否

    cmtResltView 隐藏
    提交信息进行认证 ---》请求成功显示认证结果
  }
 
 */

#pragma mark - 四 网络请求
// 1.是否认证了
- (void)requestIsCummunityIdentity:(NSString *)cmtId {
    if ([NSString isEmptyString:cmtId]) {
        XJLog(@"请求参数:communityId:%@为空操作无效",cmtId);
        return;
    }
    [XJHttpTool requestCommunityIdentityIsIdentityWithCmtId:cmtId successH:^(id responseObjc) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        XJLog(@"%@",jsonStr);
        // 1为正在审核，2审核通过，3为审核失败
        NSNumber *status = responseObjc[@"data"][@"status"];
        if ([status isEqualToNumber:@1] || [status isEqualToNumber:@2]) { //审核中、审核通过
            self.cmtResltView.hidden = NO; //显示审核状态view
            self.commitMsgView.hidden = YES; //隐藏提交信息view
            //获取认证详情，然后在给cmtResltView 这个view赋值
            [self requestCommunityIdentityDetailWithCommunityId:cmtId status:status];
        }else {//填写信息进行审核
            self.cmtResltView.hidden = YES;
            self.commitMsgView.hidden = NO;
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}
// 2.提交信息进行认证 ：这个操作放在了view层(XJCmtIdCommitMsgView)
// 3.认证详情
- (void)requestCommunityIdentityDetailWithCommunityId:(NSString *)cmtId status:(NSNumber *)status{
    if ([NSString isEmptyString:cmtId]) {
        XJLog(@"社区id为空操作无效 ");
        return;
    }
    
    [XJHttpTool requestCommunityIdentityDetailWithCommunityID:cmtId successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSDictionary *detailDic = responseObjc[@"data"][@"detail"];
            
            [self.cmtResltView configureXJCmtIdResultViewWithDetailDic:detailDic status:status];            
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}
// 4.认证列表。 不在这里操作

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
